﻿using MaterialDesignDemo.ViewModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;

namespace MaterialDesignDemo.UserControls
{
    /// <summary>
    /// ItemListControl.xaml 的交互逻辑
    /// </summary>
    public partial class ItemListControl : UserControl
    {
        public ItemListControl()
        {
            InitializeComponent();
            listview.Items.Add(new Person { Name = "张三", Age = 20, Address = "地址1" });
            listview.Items.Add(new Person { Name = "李四", Age = 32, Address = "地址2" });
            listview.Items.Add(new Person { Name = "王五", Age = 19, Address = "地址3" });
            ItemListControlViewModel itemListControlViewModel = new ItemListControlViewModel(new Person { Name = "张三", Age = 20, Address = "地址1" });
            sp.DataContext = itemListControlViewModel;
            List<Person> Persons = new List<Person>();
            Person person = new Person { Name = "张三", Age = 20, Address = "地址1" };
            Person person2 = new Person { Name = "李四", Age = 20, Address = "地址1" };
            Persons.Add(person);
            Persons.Add(person2);
            ItemListControlViewModel itemListControlViewModel2 = new ItemListControlViewModel(Persons);
            ItemControls.DataContext = itemListControlViewModel2;
            //CommandViewModel commandViewModel = new CommandViewModel();
            //commadTest.DataContext = commandViewModel;
            //dataGrid.ItemsSource = Persons;
            //templateDC.DataContext = person;
        }

        private void listview_changed(object sender, SelectionChangedEventArgs e)
        {
            ListView listview1 = sender as ListView;
            if (sender == null)
                return;
            var person = listview1?.SelectedItem as Person;
            if (person == null) return;
            MessageBox.Show($"名字：{person.Name}，地址：{person.Address}");
        }
       
        public class ObservableObject : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;

            public void RaisePropertyChanged([CallerMemberName] string propertyName = "")
            {
                Console.WriteLine($"Property changed: {propertyName}");
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public class ItemListControlViewModel : ObservableObject
        {
            private Person person;
            public Person Person
            {
                get { return person; }
                set { person = value;
                    RaisePropertyChanged(); 
                }
            }
            private List<Person> persons = new List<Person>();
            public List<Person> Persons
            {
                get { return persons; }
                set { persons = value; RaisePropertyChanged(); }
            }
            public ItemListControlViewModel(Person person)
            {
              this.person = person;
            }
            public ItemListControlViewModel(List<Person> persons)
            {
                this.persons = persons;
            }
            private string name;
            public string Name
            {
                get { return name; }
                set
                {
                    name = value;
                    RaisePropertyChanged();
                }
            }
            private int age;
            public int Age
            {
                get { return age; }
                set
                {
                    age = value;
                    RaisePropertyChanged();
                }
            }
            private string address;
            public string Address
            {
                get { return address; }
                set
                {
                    address = value;
                    RaisePropertyChanged();
                }
            }
        }

        private void slider_valueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            slider_tb.Text=(sender as Slider)?.Value.ToString("F2");
        }
    }

}
